package com.yzyz.utils.error;

import net.sf.json.JSONObject;

/**
 * 
 * @author David Sun
 * @Date   2010-3-31
 *
 */
public class ErrorBean {
	private int code;
	private String message;
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String toJson(){
		return JSONObject.fromObject(this).toString();
	}
	
	public static ErrorBean getInstance(int code) {
		ErrorBean error = new ErrorBean();
		error.setCode(code);
		error.setMessage(Error.errorMsg[code]);
		return error;
	}
	public static ErrorBean getInstance(int code, String message) {
		ErrorBean error = new ErrorBean();
		error.setCode(code);
		error.setMessage(message);
		return error;
	}
	
	/**
	 * 返回数据库生成的id
	 * @param code
	 * @param message
	 * @param id
	 * @return
	 */
	public static ErrorBean getInstance(int code, String message, int id){
		ErrorBean error = new ErrorBean();
		error.setCode(code);
		error.setMessage(message);
		error.setId(id);
		return error;
	}
	/**
	 * 返回数据库生成的id
	 * @param code
	 * @param id
	 * @return
	 */
	public static ErrorBean getInstance(int code, int id){
		ErrorBean error = new ErrorBean();
		if(code == Error.SUCCESS) {
			error.setMessage("操作成功");
		} else {
			error.setMessage(Error.errorMsg[code]);
		}
		error.setCode(code);
		error.setId(id);
		return error;
	}
	
	public static String getSuccessMsg(){
		return ErrorBean.getInstance(Error.SUCCESS, "操作成功").toJson();
	}
}
